package dao.impl;

import dao.UserDao;
import domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());

    @Override
    public User selectStudents(int id) {
        try {
            return queryRunner.query("select*from stuinfo where id= ?",new BeanHandler<>(User.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User select(String username, String password) {
        try {
            return queryRunner.query("select*from stuinfo where stuno=? and pwd = ?",new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount(String where, List<Object> params) {
        String sql = "SELECT COUNT(*) FROM stuinfo " + where;
        try {
            return queryRunner.query(sql, new ScalarHandler<>(), params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> selectByPage(int pageNum, int pageSize, String where, List<Object> params) {
        String sql = "SELECT * FROM `stuinfo` " + where + " ORDER BY id LIMIT ?,?";
        params.add((pageNum - 1) * pageSize);
        params.add(pageSize);
        try {
            return queryRunner.query(sql, new BeanListHandler<>(User.class), params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> selectAll() {
        String sql = "SELECT* FROM stuinfo  where status = 0";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deletestu(int id) {
        try {
            queryRunner.update("delete from stuinfo where id = ?",id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(User user) {
        String sql="insert into stuinfo values (null,?,?,?,?,?,?)";
        Object[] params={user.getStuname(),user.getStuno(),user.getPwd(),user.getIdcard(),user.getStartdate(),user.getStatus()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(User user) {
        String sql=" update stuinfo set stuname=?,stuno=?,pwd=?,idcard=?,startdate=?,status=? where id=? ";
        Object[] params={user.getStuname(),user.getStuno(),user.getPwd(),user.getIdcard(),user.getStartdate(),user.getStatus(),user.getId()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
